﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace SBlogCore.Common.SwaggerExtend
{
    public static class SwaggerExtensions
    {
        public static void AddSwaggerSetUp(this IServiceCollection Services)
        {
            Services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1",
                   new OpenApiInfo
                   {
                       Title = "SBlogCoreAPI",
                       Version = "v1"
                   }
                );
                //添加授权
                var schemeName = "Bearer";
                c.AddSecurityDefinition(schemeName, new OpenApiSecurityScheme
                {
                    In = ParameterLocation.Header,
                    Description = "请输入不带有Bearer的Token",
                    Name = "Authorization",
                    Type = SecuritySchemeType.Http,
                    Scheme = schemeName.ToLowerInvariant(),
                    BearerFormat = "JWT"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = schemeName
                            }
                        },
                        new string[0]
                    }
                });
            });
        }
    }
}
